package org.game.module.equipmentbo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.tools.ToolError;

import org.constant.EventConditionType;
import org.constant.EventType;
import org.error.GameErrorCode;
import org.game.constant.EquipmentConstant;
import org.game.constant.SessionConstent;
import org.game.db.bean.Hero;
import org.game.db.bean.Player;
import org.game.db.cache.HeroCache;
import org.game.db.cache.ItemCache;
import org.game.db.vo.EquipmentVO;
import org.game.manager.EquipmentManager;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventPlugin;
import org.game.protobuf.c2s.C2SEquipmentMsg;

import java.util.List;
import java.util.Map;

/**
 * Created by fxf on 2017/9/27.
 */
public class EquipmentBOImpl implements EquipmentBO {

    @Override
    public byte[] equip(ISession session, C2SEquipmentMsg.EquipMsg equipMsg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        long heroId = Long.valueOf(equipMsg.getHeroId());
        long equipmentId = Long.valueOf(equipMsg.getEquipmentId());
        int position = equipMsg.getPosition();

        Hero hero = HeroCache.me().getById(heroId);
        ToolError.isAndTrue(GameErrorCode.HERO_ID_ERR, "heroId错误", hero == null);
        EquipmentVO equipment = (EquipmentVO) ItemCache.me().getById(equipmentId);
        ToolError.isAndTrue(GameErrorCode.EQUIPMENT_ID_ERR, "equipmentId错误", equipment == null);
        ToolError.isAndTrue(GameErrorCode.ALREADY_EQUIP, "灵装已被装备", equipment.getPosition() != EquipmentVO.NO_EQUIP_POSITION);
        ToolError.isAndTrue(GameErrorCode.POSITION_ERR, "装备位置错误", position > EquipmentConstant.EQUIP_POSITION_NUM || position <= 0);

        return EquipmentManager.me().equip(player, hero, equipment, position);
    }

    @Override
    public byte[] takeOff(ISession session, C2SEquipmentMsg.TakeOffEquipmentMsg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        long heroId = Long.valueOf(msg.getHeroId());
        int position = msg.getPosition();

        Hero hero = HeroCache.me().getById(heroId);
        ToolError.isAndTrue(GameErrorCode.HERO_ID_ERR, "heroId错误", hero == null);
        ToolError.isAndTrue(GameErrorCode.POSITION_ERR, "装备位置错误：position="+position, position > EquipmentConstant.EQUIP_POSITION_NUM || position <= 0);
        ToolError.isAndTrue(GameErrorCode.POSITION_NO_EQUIPMENT, "该位置没有装备", hero.getHeroProxy().getEquipment().get(position) == null);

        return EquipmentManager.me().takeOff(player, hero, position);
    }

    @Override
    public byte[] changeSpecialAttr(ISession session, C2SEquipmentMsg.ChangeSpecialAttrMsg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        long sourceEquipmentId = Long.valueOf(msg.getSourceEquipmentId());
        int oldAttrIndex = msg.getOldAttrIndex();
        long costEquipmentId = Long.valueOf(msg.getCostEquipmentId());

        //数据校验
        EquipmentVO sourceEquipment = (EquipmentVO) ItemCache.me().getById(sourceEquipmentId);
        ToolError.isAndTrue(GameErrorCode.EQUIPMENT_ID_ERR, "equipmentId错误", sourceEquipment == null);
        ToolError.isAndTrue(GameErrorCode.SPECIAL_ATTR_INDEX_ERR,"",oldAttrIndex<0||oldAttrIndex>4|| sourceEquipment.getSpecialAttrList().size() <= oldAttrIndex);
        EquipmentVO costEquipment = (EquipmentVO) ItemCache.me().getById(costEquipmentId);
        ToolError.isAndTrue(GameErrorCode.EQUIPMENT_ID_ERR, "equipmentId错误", costEquipment == null);
        ToolError.isAndTrue(GameErrorCode.ALREADY_EQUIP, "灵装被装备，不能消耗", costEquipment.getHeroId()!=0);
        ToolError.isAndTrue(GameErrorCode.HAVE_REPLACE_ATTR, "有临时属性，不能洗炼", sourceEquipment.getTempSpecialAttr() != null);



        return EquipmentManager.me().changeSpecialAttr(player,sourceEquipment,oldAttrIndex,costEquipment);
    }

    @Override
    public byte[] upgrade(ISession session, C2SEquipmentMsg.UpgradeMsg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        long equipmentId = Long.valueOf(msg.getEquipmentId());
        List<String> costEquipmentIdList = msg.getCostEquipmentIdList();

        EquipmentVO sourceEquipment = (EquipmentVO) ItemCache.me().getById(equipmentId);
        ToolError.isAndTrue(GameErrorCode.EQUIPMENT_ID_ERR, "equipmentId错误", sourceEquipment == null);

        List<EquipmentVO> costEquipmentList = Lists.newArrayList();
        costEquipmentIdList.forEach(id->{
            EquipmentVO costEquipment = (EquipmentVO) ItemCache.me().getById(Long.valueOf(id));
            ToolError.isAndTrue(GameErrorCode.EQUIPMENT_ID_ERR, "强化，被消耗的equipmentId错误", costEquipment == null);
            ToolError.isAndTrue(GameErrorCode.ALREADY_EQUIP, "灵装已被装备，不能消耗", costEquipment.getHeroId() != 0);
            costEquipmentList.add(costEquipment);
        });
        return EquipmentManager.me().upgrade(player,sourceEquipment,costEquipmentList);
    }

    @Override
    public byte[] replaceSpecialAttr(ISession session, C2SEquipmentMsg.ReplaceSpecialAttrMsg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        long equipmentId = Long.valueOf(msg.getEquipmentId());
        boolean replace = msg.getReplace();

        EquipmentVO equipment = (EquipmentVO) ItemCache.me().getById(equipmentId);
        ToolError.isAndTrue(GameErrorCode.EQUIPMENT_ID_ERR, "equipmentId错误", equipment == null);
        ToolError.isAndTrue(GameErrorCode.NO_REPLACE_ATTR, "没有临时属性，无法替换", equipment.getTempSpecialAttr() == null);

        return EquipmentManager.me().replaceSpecialAttr(player,equipment,replace);
    }
}
